package _go

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

//基本思路 还是层序遍历  只是把一层的结果输出到临时数组中 判断一下奇偶确实放入数组中的顺序
func levelOrder(root *TreeNode) [][]int {
	res := [][]int{}
	if root == nil {
		return [][]int{}
	}

	//层高
	level := 0
	var queue []*TreeNode

	queue = append(queue, root)

	for len(queue) > 0 {
		tmp := []*TreeNode{}
		tmpary := make([]int, len(queue))
		for i := 0; i < len(queue); i++ {
			if level%2 == 0 {
				tmpary[i] = queue[i].Val
			} else {
				tmpary[len(queue)-i-1] = queue[i].Val
			}
			if queue[i].Left != nil {
				tmp = append(tmp, queue[i].Left)
			}
			if queue[i].Right != nil {
				tmp = append(tmp, queue[i].Right)
			}
		}
		res = append(res, tmpary)
		queue = tmp
		level++
	}
	return res
}
